<%@ page language="java" import="org.json.*,java.sql.*,java.text.*,java.util.*,com.microsoft.sqlserver.jdbc.*,java.io.*,web.rec.*" 
contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"  %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<% 
	Connection conn = ResourceTool.getConnection();
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<table>
<%
			try {
			// Create the prepared statement and use it to
			// INSERT the student attrs INTO the Student table.
			Statement stmt = conn.createStatement();
			String action = request.getParameter("action");
			String section_id = request.getParameter("section_id");
			String start_date = request.getParameter("start_date");
			String end_date = request.getParameter("end_date");
			String current_date;
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			String [] week = {"Sunday","Monday","Tuesday","Wednesday","Thursday", "Friday","Saturday"};
			if (action != null && action.equals("submit"))
			{
				Calendar start = Calendar.getInstance();
				start.setTime(dateFormat.parse(start_date));
				Calendar end = Calendar.getInstance();
				end.setTime(dateFormat.parse(end_date));
				String start_time;
				String end_time;
				int weekday;
				weekday = start.get(Calendar.DAY_OF_WEEK) - 1;
				while(start.compareTo(end)<=0)
				{
					current_date = dateFormat.format(start.getTime());
					weekday = start.get(Calendar.DAY_OF_WEEK) - 1;
					if(weekday == 0 || weekday == 6)
					{
						start.add(Calendar.DATE, 1);
						continue;
					}
					for (int i = 8; i<= 19;i++)
					{
						start_time = i + "00";
						end_time = (i+1) + "00";
						String sql = "select * from meeting,StudentTakingSection where "
								   + "meeting.section_id = StudentTakingSection.section_id and "
								   + "meeting.weekday = " + weekday + " and "
								   + "meeting.start_time >= " + start_time + " and "
								   + "meeting.end_time < " + end_time + " and "
								   + "meeting.start_date <= '" + current_date + "' and "
								   + "meeting.end_date >= '" + current_date + "' and "
								   + "StudentTakingSection.student_id in ("
								   + "select student_id from StudentTakingSection where "
								   + "StudentTakingSection.section_id  = '" + section_id + "')";
						System.out.println(sql);
						ResultSet rs = stmt.executeQuery(sql);
						if (!rs.next())
						{
						%>
						<tr>
						<td> <%= start.get(Calendar.MONTH) + 1 %> <%= start.get(Calendar.DAY_OF_MONTH) %> <%= week[weekday] %> <%= i %>:00 - <%= i+1 %>:00 </td>
						</tr>
						<%
						}
					}
					start.add(Calendar.DATE, 1);
				}
			}
		} catch (SQLException sqle) {
					out.println(sqle.getMessage());
		} catch (Exception e) {
					out.println(e.getMessage());
	}    
%>
</table>
</body>
</html>